package com.learn.transform;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @create: 2023-04-18 15:40
 * @author: Mr.Du
 * --------------
 * @notes: 用process实现一个类似fliter的效果
 **/
public class TransformProcessFunctionDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);
        DataStreamSource<String> input = env.socketTextStream("node1", 9999);
        input.filter(new FilterFunction<String>() {
            @Override
            public boolean filter(String s) throws Exception {
                return s.trim().length() > 0;
            }
        }).printToErr("filter>>>>");

        input.process(new ProcessFunction<String, String>() {
            @Override
            public void processElement(String s, ProcessFunction<String, String>.Context ctx, Collector<String> out) throws Exception {
                if(s.trim().length() > 0) {
                    out.collect(s);
                }
            }
        }).print("process>>>>");

        env.execute();
    }
}
